﻿using CommonLib;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using 电脑信息采集台账导出.Models;

namespace 电脑信息采集台账导出
{
    internal class MainWindowViewModel : ObservableObject
    {
        private 填写信息Model tXXX;

        public 填写信息Model TXXX
        {
            get { return tXXX; }
            set { tXXX = value; OnPropertyChanged(); }
        }
        private 软件信息Model rJXX;

        public 软件信息Model RJXX
        {
            get { return rJXX; }
            set { rJXX = value; OnPropertyChanged(); }
        }
        private 系统信息Model xTXX;

        public 系统信息Model XTXX
        {
            get { return xTXX; }
            set { xTXX = value; OnPropertyChanged(); }
        }

        public ObservableCollection<string> JSJLXList { get; set; }
        public ObservableCollection<string> WLLXList { get; set; }
        public ObservableCollection<string> OEMList { get; set; }

        public RelayCommand StartCommand { get; set; }
        public RelayCommand DataOutPutCommand { get; set; }
        public RelayCommand OpenNetCommand { get; set; }

        public MainWindowViewModel()
        {
            (List<ReadInfo> 数据库数据, List<ReadInfo> 下拉框数据) read = ExtUrl.初始化读取软件数据库信息();
            TXXX = new 填写信息Model();
            RJXX = new 软件信息Model();
            XTXX = new 系统信息Model();
            JSJLXList = new ObservableCollection<string>();
            WLLXList = new ObservableCollection<string>();
            OEMList = new ObservableCollection<string>();

            RJXX.计算机类型 = read.下拉框数据[0].计算机类型List[0];
            RJXX.网络类型 = read.下拉框数据[0].网络类型List[0];

            read.下拉框数据.ForEach(x =>
            {
                x.计算机类型List.ForEach(z => JSJLXList.Add(z));
                x.网络类型List.ForEach(z => WLLXList.Add(z));
                x.OEMList.ForEach(z => OEMList.Add(z));
            });

            OpenNetCommand = new RelayCommand(() =>
            {
                // 打开网络和共享中心
                Process.Start("ncpa.cpl");
            });

            StartCommand = new RelayCommand(() =>
            {
                StringBuilder bg = new StringBuilder();
                StringBuilder sj = new StringBuilder();
                StringBuilder sd = new StringBuilder();
                List<string> appNames = ExtUrl.获取电脑上所有软件();
                appNames.ForEach(x =>
                {
                    var res = read.数据库数据.First().软件List.Find(z => x.ToLower().Contains(z.Name.ToLower()));
                    if (res != null)
                    {
                        /*
                         1 为办公
                         2 为设计
                         3 为杀毒
                         */
                        switch (res.ID)
                        {
                            case 1:
                                bg.Append($"{x}|");
                                break;
                            case 2:
                                sj.Append($"{x}|");
                                break;
                            case 3:
                                sd.Append($"{x}|");
                                break;
                            default:
                                break;
                        }
                    }
                });

                if (bg.Length>0)
                {
                    RJXX.办公软件 = bg.Remove(bg.Length - 1, 1).ToString();
                }
                if (sj.Length > 0)
                {
                    RJXX.设计软件 = sj.Remove(sj.Length - 1, 1).ToString();
                }
                if (sd.Length > 0)
                {
                    RJXX.杀毒软件 = sd.Remove(sd.Length - 1, 1).ToString();
                }


                (string ip, string mac, string sys) re = ExtUrl.获取IP_MAC地址_系统版本();

                XTXX.IP地址 = re.ip;
                XTXX.Mac地址 = re.mac;
                XTXX.操作系统 = re.sys;
                XTXX.操作系统安装时间 = ExtUrl.获取操作系统的安装时间();
            });

            DataOutPutCommand = new RelayCommand(() =>
            {
                ExtUrl.生成Excel(new AllData()
                {
                    IP地址 = XTXX.IP地址,
                    Mac地址 = XTXX.Mac地址,
                    操作系统 = XTXX.操作系统,
                    使用部门 = TXXX.使用部门,
                    房间号 = TXXX.房间号,
                    姓名 = TXXX.姓名,
                    备注 = TXXX.备注,
                    办公软件 = RJXX.办公软件,
                    设计软件 = RJXX.设计软件,
                    杀毒软件 = RJXX.杀毒软件,
                    计算机类型 = RJXX.计算机类型,
                    网络类型 = RJXX.网络类型,
                    OEM = RJXX.OEM,
                    操作系统安装时间 = XTXX.操作系统安装时间
                });

                MessageBox.Show(Application.Current.MainWindow,"导出成功!");
            });

        }
    }
}

